package com.traffic.api.sms;

import com.traffic.api.entity.SmsInfo;
import com.traffic.api.service.SMSInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;

/**
 * Created by churcy on 2017/3/2.
 */
@Component
public class SMSUtil {
    @Autowired
    private SMSInfoService smsInfoService;

    public String sendSMS (String mobilePhone,String content,String code) {
        String result = null;
        KXTSmsSDK kxtsms = new KXTSmsSDK();
        kxtsms.init("120.76.25.160", 7788, "ybtest", "520620",716);
        try {
            content = URLEncoder.encode(content,"UTF-8");//URL编码 UTF-8方式
            result = kxtsms.send(mobilePhone,content,code);
            SmsInfo smsInfo = new SmsInfo();
            smsInfo.setPhone(mobilePhone);
            smsInfo.setSmsId(code);
            smsInfoService.save(smsInfo);
        } catch (Exception e) {

        }
        return result;
    }

    public static void main(String[] args) {
        SMSUtil smsUtil = new SMSUtil();

        smsUtil.sendSMS("18627170706","测试短信验证码【测试】","666666");
    }
}
